package top.ytazwc.normal;

import top.ytazwc.take.redis.RedisUtils;
import top.ytazwc.take.redis.cluster.EGM;
import top.ytazwc.take.redis.cluster.IIR;

import java.util.concurrent.TimeUnit;

/**
 * @author 花木凋零成兰
 * @title CacheClusterServiceImpl
 * @date 2024/4/28 16:18
 * @package top.ytazwc.normal
 * @description 升级后的服务 可以使用多套集群 同时兼容以前的Redis单体服务
 */
public class CacheClusterServiceImpl implements CacheService {

    // Redis单体服务
    private RedisUtils redisUtils = new RedisUtils();
    // EGM集群服务
    private EGM egm = new EGM();
    // IIR集群服务
    private IIR iir = new IIR();

    @Override
    public String get(String key, int redisType) {
        if (1 == redisType) {
            return egm.gain(key);
        }
        if (2 == redisType) {
            return iir.get(key);
        }
        return redisUtils.get(key);
    }

    @Override
    public void set(String key, String value, int redisType) {
        if (1 == redisType) {
            egm.set(key, value);
            return;
        }
        if (2 == redisType) {
            iir.set(key, value);
            return;
        }
        redisUtils.set(key, value);
    }

    @Override
    public void set(String key, String value, long timeout, TimeUnit timeUnit, int redisType) {
        if (1 == redisType) {
            egm.setEx(key, value, timeout, timeUnit);
            return;
        }
        if (2 == redisType) {
            iir.setExpire(key, value, timeout, timeUnit);
            return;
        }
        redisUtils.set(key, value, timeout, timeUnit);
    }

    @Override
    public void del(String key, int redisType) {
        if (1 == redisType) {
            egm.delete(key);
            return;
        }
        if (2 == redisType) {
            iir.del(key);
            return;
        }
        redisUtils.del(key);
    }
}
